<template>
  <div class="draggable_content">
    <div class="header">
      <span>{{ props.title }}({{ data.list.length }})</span>
      <div class="header-right">
        <slot name="headright"></slot>
      </div>
    </div>
    <div class="content">
      <slot name="list" :spaceArr="data.list"></slot>
      <!-- <draggable v-model="data.list" group="people" @start="drag = true" @end="onEnd" item-key="id">
        <template #header></template>
        <template #item="{ element, index }">
          <div class="child" @click="handelClickChildItem(element, index)" :class="index == data.num ? 'active' : ''">
            <div class="top">
              <div class="top-left text-over" style="max-width: 140px">
                <el-input
                  v-model="element.spaceName"
                  ref="autoFocus"
                  maxlength="20"
                  v-if="element.edit"
                  @blur="handelBlur(element)"
                  @focus="onFocus"
                ></el-input>
                <span v-else :title="element.spaceName">{{ element.spaceName }}</span>
              </div>
              <div class="top-right">
                <el-popover placement="bottom" title="" :width="200" trigger="hover" :popper-class="'template-actionmore'">
                  <template #default>
                    <div class="action-wrap">
                      <p v-for="(item, i) in data.operateListChildFile" :key="i" @click="handelClickOperate(element, item)">
                        <el-button link :type="item.command === 'd' ? 'danger' : 'default'">{{ item.name }}</el-button>
                      </p>
                    </div>
                  </template>
                  <template #reference>
                    <span class="el-dropdown-link">
                      <el-icon><MoreFilled /></el-icon>
                    </span>
                  </template>
                </el-popover>
              </div>
            </div>
          </div>
        </template>
      </draggable> -->
    </div>
  </div>
</template>
<script setup>
// 资源导入
import { reactive, defineProps, defineEmits, watch, ref, nextTick } from "vue";
import draggable from "vuedraggable";
// import { ElMessage } from "element-plus";
// 变量定义
const autoFocus = ref();
const emits = defineEmits(["clickItem", "copy", "blurs", "delet", "dragEnd"]);
const props = defineProps({
  title: {
    type: String,
    default() {
      return "";
    }
  },
  list: {
    type: Array,
    default() {
      return [];
    }
  }
});
const data = reactive({
  list: [],
  // 选中的数据
  selectedData: {},
  num: 0,
  childNum: -1,
  // 拖动列表数据
  myArray: [],
  operateList: [
    { id: 1, name: "添加子文件", command: "a" },
    { id: 2, name: "编辑", command: "b" },
    { id: 3, name: "复制", command: "c" },
    { id: 4, name: "删除", command: "d" }
  ],
  operateListChildFile: [
    { id: 2, name: "编辑", command: "b" },
    { id: 3, name: "复制", command: "c" },
    { id: 4, name: "删除", command: "d" }
  ],
  engineeringTypeList: []
});
// 方法定义

const onFocus = event => {
  event.target.select();
  // console.log(909090, event);
};

const onEnd = () => {
  // drag = false
  console.log("拖拽结束center", data.list);
  emits("dragEnd", data.list);
};

// 更多操作
const handelClickOperate = (element, item) => {
  const command = item.command;
  if (command === "b") {
    element.edit = true;
    nextTick(() => {
      // autoFocus.value.select();
      // autoFocus.value.focus();
    });
  } else if (command === "c") {
    emits("copy", element);
  } else if (command === "d") {
    emits("delet", element);
  }
  console.log(element);
};
// 失去焦点时保存当前文件的名称并不让在编辑
const handelBlur = element => {
  element.edit = false;
  emits("blurs", element);
};
const handelClickChildItem = (item, i) => {
  console.log(item, i);
  data.num = i;
  emits("clickItem", item);
};
defineExpose({
  handelBlur,
  handelClickOperate,
  onFocus
});
// 生命周期
// onMounted(() => {
//   getFilieList();
// });
watch(
  () => props.list,
  arr => {
    data.list = [...arr];
  },
  { deep: true, immediate: true }
);
// 监听
</script>
<style scoped lang="scss">
.active {
  background: #d9e8ff !important;
  box-sizing: border-box;
  :deep(.el-dropdown-link) {
    color: #007dff;
  }
}
:deep(.el-dropdown-link) {
  i {
    transform: rotate(90deg);
  }
}
.active1 {
  background-color: #d9e8ff !important;
}
.file_operate {
  display: flex;
  justify-content: space-between;
  width: 100%;
  &_right {
    display: flex;
    align-items: center;
  }
}
.child {
  // background-color: #fff;
  // box-shadow: 0px 0px 10px 0px rgba(134, 144, 156, 0.1);
  padding: 10px 0;
  overflow: hidden;
  box-sizing: border-box;
  cursor: pointer;
  .top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-sizing: border-box;
    .top-left {
      font-size: 14px;
      color: #191919;
      :deep(.el-input__wrapper) {
        background-color: white;
      }
    }
  }
  .botton {
    display: flex;
    align-items: center;
    justify-content: space-between;
    .botton_left {
      font-size: 12px;
      color: #86909c;
    }
  }
}
.draggable_content {
  //   height: calc(100% - 40px);
  //   background-color: #fff;
  // border-radius: 8px;
  overflow: hidden;
  max-height: calc(100% - 170px);
  overflow-y: auto;
  .header {
    font-size: 14px;
    color: #3d3d3d;
    padding: 20px 20px 10px 20px;
    overflow: hidden;
    display: flex;
    justify-content: space-between;
    align-items: center;
    span {
      position: relative;
      font-size: 14px;
      color: #3d3d3d;
    }
  }
}
// 拖拽外层内容css
.content {
  height: calc(100% - 50px);
  // overflow-y: scroll;
  background: #f2f3f5;
}
.file {
  width: 100%;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
  box-sizing: border-box;
  img {
    width: 16px;
  }
  span {
    margin-left: 10px;
  }
}
.child_file {
  padding-left: 40px;
  width: 100%;
  height: 44px;
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #000;
  box-sizing: border-box;
  span {
    margin-left: 10px;
  }
  img {
    width: 16px;
  }
}
.add_file {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
}
</style>
